﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Web.Http.Results;
using System.Web.Mvc; // needed for lambda includes
using System.Web;

using TheCup.ApiControllers;



namespace TheCup.Controllers
{
    public class ListController : Controller
    {
        ApiControllers.ListController lc = new ApiControllers.ListController();
        ApiControllers.SeasonController s = new ApiControllers.SeasonController();

        public ActionResult OrderOfMerit(string Date)
        {
            DateTime date = DateTime.Now;
            if (Date == null || !DateTime.TryParse(Date, out date))
            {
                date = DateTime.Now;
            }

            List<OrderOfMeritListEntry> l = lc.GetOrderOfMerit(date.ToShortDateString()).ToList<OrderOfMeritListEntry>();
            ViewBag.Seasonlist = s.Get();

            Season thisseason = s.Get(date);
            if (thisseason != null)
            {
                ViewBag.SeasonName = thisseason.Name;
            }
            return View(l);
        }

        public ActionResult Eclectic(string Date)
        {
            DateTime date = DateTime.Now;
            if (Date == null || !DateTime.TryParse(Date, out date))
            {
                date = DateTime.Now;
            }

            List<EclecticListEntry> l = lc.GetEclectic(date.ToShortDateString()).ToList<EclecticListEntry>();
            ViewBag.Seasonlist = s.Get();

            Season thisseason = s.Get(date);
            if (thisseason != null)
            {
                ViewBag.SeasonName = thisseason.Name;
            }
            return View(l);
        }

        public ActionResult EclecticStableford(string Date)
        {
            DateTime date = DateTime.Now;
            if (Date == null || !DateTime.TryParse(Date, out date))
            {
                date = DateTime.Now;
            }

            List<EclecticStablefordListEntry> l = lc.GetEclecticStableford(date.ToShortDateString()).ToList<EclecticStablefordListEntry>();
            ViewBag.Seasonlist = s.Get();

            Season thisseason = s.Get(date);
            if (thisseason != null)
            {
                ViewBag.SeasonName = thisseason.Name;
            }
            return View(l);
        }

        public ActionResult EclecticWorst(string Date)
        {
            DateTime date = DateTime.Now;
            if (Date == null || !DateTime.TryParse(Date, out date))
            {
                date = DateTime.Now;
            }

            List<EclecticListEntry> l = lc.GetEclecticWorst(date.ToShortDateString()).ToList<EclecticListEntry>();
            ViewBag.Seasonlist = s.Get();

            Season thisseason = s.Get(date);
            if (thisseason != null)
            {
                ViewBag.SeasonName = thisseason.Name;
            }
            return View(l);
        }

        public ActionResult EclecticNetto(string Date)
        {
            DateTime date = DateTime.Now;
            if (Date == null || !DateTime.TryParse(Date, out date))
            {
                date = DateTime.Now;
            }

            List<EclecticListEntry> l = lc.GetEclecticNetto(date.ToShortDateString()).ToList<EclecticListEntry>();
            ViewBag.Seasonlist = s.Get();

            Season thisseason = s.Get(date);
            if (thisseason != null)
            {
                ViewBag.SeasonName = thisseason.Name;
            }
            return View(l);
        }

        public ActionResult WorldRanking(string Date)
        {
            DateTime date = DateTime.Now;
            if (Date == null || !DateTime.TryParse(Date, out date))
            {
                date = DateTime.Now;
            }
            ViewBag.FromDate = date;

            List<WorldRankingListEntry> l = lc.GetWorldRanking(date.ToShortDateString()).ToList<WorldRankingListEntry>();

            return View(l);
        }

        public ActionResult Trophy(string Date)
        {
            DateTime date = DateTime.Now;
            if (Date == null || !DateTime.TryParse(Date, out date))
            {
                date = DateTime.Now;
            }

            ViewBag.Seasonlist = s.Get();

            Season thisseason = s.Get(date);
            if (thisseason != null)
            {
                ViewBag.SeasonName = thisseason.Name;
            }

            List<TrophyListEntry> l = lc.GetTrophy(date.ToShortDateString()).ToList<TrophyListEntry>();
            return View(l);
        }

        public ActionResult BestTenRounds(string date)
        {
            ViewBag.Seasonlist = s.Get();

            Season thisseason = this.GetSeason(date);
            if (thisseason != null)
            {
                ViewBag.SeasonName = thisseason.Name;
            }

            var rounds = lc.GetTenBestRounds(date);
            return View(rounds);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        private Season GetSeason(string date)
        {
            DateTime dd = string.IsNullOrEmpty(date) ? DateTime.Today : DateTime.Parse(date);

            return s.Get(dd);
        }
    }
}
